class Freshman < ActiveRecord::Base
  validates_presence_of :first_name, :last_name, :hometown, :athena_name
  validates_uniqueness_of :athena_name
  
  has_many :freshman_jaunts
  has_many :favorite_freshmen
  has_many :house_visits
  has_many :overnights
  has_many :desk_action_aggregates
  has_many :entrances, :through => :house_visits
  has_many :exits, :through => :house_visits
  has_many :departures, :through => :freshman_jaunts
  has_many :jaunt_returns, :through => :freshman_jaunts
  has_many :overnight_starts, :through => :overnights
  has_many :wakeups, :through => :overnights
  has_many :desk_actions, :through => :desk_action_aggregates
  
  
  def jaunt_depart(jaunt_id, departure_time, recording_user_id)
    this_jaunt = Jaunt.find(jaunt_id)
    if current_jaunt.nil?
      if current_house_visit.house_id == this_jaunt.house_id
        this_freshman_jaunt = freshman_jaunts.create(:jaunt_id => jaunt_id)
        this_freshman_jaunt.create_departure(	:effective_at => departure_time,
        										:recorded_at => Time.now,
        										:recorded_by => recording_user_id )
        this_freshman_jaunt.save!
      else
        return "freshman not checked into this jaunt's house"
      end
    else
      return "freshman already on a jaunt"
    end
  end
  
  def jaunt_return(return_time, recording_user_id)
    jaunt_to_end = current_jaunt
    unless jaunt_to_end.nil?
   	  jaunt_to_end.create_jaunt_return(	:effective_at => return_time,
   	  									:recorded_at => Time.now,
   	  									:recorded_by => recording_user_id)
  	  jaunt_to_end.save! 
  	else
  	  return "freshmen isn't on a jaunt"
  	end
  end
  
  def enter_house(house_id, enter_time, recording_user_id)
  	if current_house_visit.nil?
  	  this_house_visit = house_visits.create(:house_id => house_id)
  	  this_house_visit.create_entrance(:effective_at => enter_time, :recorded_at => Time.now, :recorded_by => recording_user_id)
  	  this_house_visit.save!
  	else
  	  return "freshman is already checked into a house"
  	end
  end
  
  def exit_house(exit_time, destination, recording_user_id)
    visit_to_end = current_house_visit
    unless visit_to_end.nil?
      if destination.to_s.length > 0
		  visit_to_end.create_exit(	:effective_at => exit_time,
									:recorded_at => Time.now,
									:recorded_by => recording_user_id,
									:destination => destination)
		  visit_to_end.save!
	  else
	    return "specify a destination"
	  end
	else
	  return "freshman isn't checked into a house"
	end
  end
  
  def start_overnight(house_id, wakeup_time, recording_user_id)
    if !current_house_visit.nil? and current_house_visit.house_id = house_id
      if current_overnight.nil?
        this_overnight=overnights.create(:house_id => house_id, :wakeup_time => wakeup_time)
        this_overnight.create_overnight_start(:effective_at => Time.now, :recorded_at => Time.now, :recorded_by => recording_user_id)
        this_overnight.save!
      else
        return "freshman is already overnighting"
      end
    else
      return "can't set overnight"
    end
  end
  
  def wakeup(effective_at, recording_user_id)
    overnight_to_end=current_overnight
    unless overnight_to_end.nil?
      overnight_to_end.create_wakeup(:effective_at=>effective_at, :recorded_at => Time.now, :recorded_by => recording_user_id)
      overnight_to_end.save!
    else
      return "freshman isn't overnighting"
    end
  end
    
  def current_house_visit
    all_house_visits = house_visits.find(:all)
    all_house_visits.find { |this_house_visit|
      this_house_visit.currently_active
    }
  end
  
  def current_overnight
    all_overnights = overnights.find(:all)
    all_overnights.find { |this_overnight|
      this_overnight.currently_active
    }
  end
 
  def current_jaunt
    all_freshman_jaunts = freshman_jaunts.find(:all)
    all_freshman_jaunts.find { |this_freshman_jaunt|
      this_freshman_jaunt.currently_active
    }
  end
  
  
  def full_name
    first_name + " " + last_name
  end
  
end
